package livesun.justiceeternal.view.titlebar;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;

import livesun.justiceeternal.R;


/**
 * 类描述：一个示范的TitleBar
 * 创建人：livesun
 * 创建时间：2017/8/29
 * 修改人：
 * 修改时间：
 * github：https://github.com/livesun
 */

public class DefultTitleBar extends AbsTitleBar<DefultTitleBar.DefultTitleBarParams> {


    private DefultTitleBar(DefultTitleBarParams params) {
        super(params);
    }

    public void setTitle(CharSequence text){
        setText(text, R.id.middle_title);
    }

    public void setRightTitle(CharSequence text){
        setText(text,R.id.right_title);
    }

    public void setLeftEvent(View.OnClickListener listener){
        if(listener!=null){
            setEvent(R.id.left_back,listener);
        }
    }

    public void setLeftIcon(int resId){
        setLeftIcon(R.id.left_back,resId);
    }

    public void setRightIcon(){

    }
    /**
     * titleBar的布局id
     *
     * @return
     */
    @Override
    public int layoutId() {
        return R.layout.defult_titlebar;
    }

    /**
     * 绑定View
     */
    @Override
    public void bindView() {
        setText(getParams().middleTitle,R.id.middle_title);
        setText(getParams().rightTitle,R.id.right_title);
        setBackground(R.id.title_bar_root,getParams().backgroundColor);
        setLeftIcon(R.id.left_back,getParams().leftIconResId);
        setEvent(R.id.back,getParams().leftClickLintener);
        setEvent(R.id.right_title,getParams().rightClickLintener);
        isTranslucentUtils(R.id.title_bar_root,getParams().statusColor,getParams().isDark);
    }

    /**
     * bulider
     */
    public static class Builder extends AbsTitleBar.Builder{

        private DefultTitleBarParams P;

        /**
         * 默认使用DecorView的第一个子控件做为TitleBar的父控件
         * @param context
         */
        public Builder(Activity context) {
            super(context);
            P = new DefultTitleBarParams(context,null);
        }

        public Builder(Activity context, ViewGroup parent) {
            super(context, parent);
            P = new DefultTitleBarParams(context,parent);
        }

        @Override
        public AbsTitleBar creat() {
            return new DefultTitleBar(P);
        }

        //==========设置所有参数===========
        //中间的标题
        public Builder setTitle(CharSequence middleTitle){
            P.middleTitle=middleTitle;
            return this;
        }
        //右边的标题--默认不显示
        public Builder setRightTitle(CharSequence rightTitle){
            P.rightTitle=rightTitle;
            return this;
        }

        //设置左边的图片
        public Builder setLeftIcon(int leftIconResId){
            P.leftIconResId=leftIconResId;
            return this;
        }

        //设置背景
        public Builder setBackground(Bitmap background){
            P.background=background;
            return this;
        }
        //设置背景颜色
        public Builder setBackground(int backgroundColor){
            P.backgroundColor=backgroundColor;
            return this;
        }
        public Builder setReightClickListener(View.OnClickListener listener){
            P.rightClickLintener =listener;

            return this;
        }

        public Builder setLeftClickListener(View.OnClickListener listener){
            P.leftClickLintener =listener;

            return this;
        }

        /**
         * 状态栏是否为黑色字体
         * true 为黑色
         * 默认为false
         * @param isDark
         * @return
         */
        public Builder setDark(boolean isDark){
            P.isDark=isDark;
            return this;
        }

        public Builder setStatusColor(int statusColor){
            P.statusColor=statusColor;
            return this;
        }
    }

    /**
     * 参数
     */
    public static class DefultTitleBarParams extends AbsTitleBarParams{
        CharSequence middleTitle=null;
        CharSequence rightTitle=null;
        int leftIconResId=0;
        Bitmap background=null;
        int backgroundColor=0;
        boolean isDark=false;
         View.OnClickListener rightClickLintener;
         View.OnClickListener leftClickLintener =new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        };
        int statusColor= Color.parseColor("#369cfc");
        public DefultTitleBarParams(Activity activity, ViewGroup parent) {
            super(activity, parent);
        }
    }
}
